<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ruby SMTP Class: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby smtp class,ruby study notes,free ruby programming guide,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Ruby helps programmers have more fun!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/ruby_gurus.html" title="Interviews I took of Ruby Gurus around the world">Gurus</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="/other/ruby_video_search.html" title="Watch videos on Ruby/Rails">Videos</a></li>
            <li><a href="/blog/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>SMTP Class</h1>


                <!--
                <div class="container">
                  <div class="adsense">
                  <strong>Note: This topic has been re-written on 23rd Nov. 2007.</strong>
                  </div>
                </div>
                -->

                <p>E-mail is still one of the most important and popular technologies used online.</p>

                <p>Let's build a small SMTP client using the <strong><a href="http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html">Net::SMTP</a></strong> class in Ruby. The program p070rubysmtp.rb is the SMTP client and the explanation follows.</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # p070rubysmtp.rb
                require 'net/smtp'
                user_from = "superman@world.com"
                user_to = "batman@world.com"
                the_email = "From: superman@world.com\nSubject: Hello\n\nEmail by Ruby in Pune.\n\n"
                # handling exceptions
                begin
                  Net::SMTP.start('localhost', 25) do |smtpclient|
                    smtpclient.send_message(the_email, user_from, user_to)
                  end
                rescue Exception => e
                  print "Exception occured: " + e
                end
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p><strong>Explanation of the p070rubysmtp.rb code:</strong></p>

                <ul>
                  <li>The <strong>Net::SMTP</strong> library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. For SMTP, I am referring to Request for Comments (RFC's) 1869, 2554, 2821, 2822 and 3207. The <strong>Net::SMTP</strong> library does NOT provide functions to compose internet mails. You must create them by yourself.</li>
                  <li>The line <strong>Net::SMTP.start</strong> assumes that you have an email server that you can send messages through. It could be the email server at your company or your internet service provider. You will need the host name and port number which you can usually find in your email client by looking at your account settings for the values stored under outgoing SMTP server. On Windows, I prefer to download and use the <a href="http://www.softstack.com/freesmtp.html"><em>Free SMTP Server</em></a> for testing. Connect your PC to Internet, then <strong>start</strong> the Free SMTP Server program. The program will try to detect your provider's DNS server. If a DNS server is available, and the standard SMTP port #25 is free the program will become ready to process your mail. If it is not possible to detect provider's DNS server, the program will prompt you to enter any DNS server you know, after that if the server exists the program will become ready to process mail. If the port #25 is busy by another local SMTP server/service, the program will show you an error message. You can specify any DNS server or SMTP port by using the button Options, which is situated on the Toolbar. You should use the word "localhost" as the SMTP server host in your mail program.</li>
                  <li>The <strong>Net::SMTP.start</strong> method creates a new <strong>Net::SMTP</strong> object, opens a TCP connection and connects to the server. 'localhost' is the IP address of your SMTP server and the port used is 25.</li>
                  <li>Since we have called with a block, the newly-opened <strong>Net::SMTP</strong> object is yielded to the block, and automatically closed when the block finishes.</li>
                  <li>In the line <strong>smtpclient.send_message(the_email, user_from, user_to)</strong> the_email is the message to be sent. Single CR and LF found in the string, are converted to the CRLF pair. You cannot send a binary message with this method. the_email should include both the message headers and body. user_from is a String representing the source mail address. user_to is a String or Array of Strings, representing the destination mail address or addresses.</li>
                  <li>If you have more than one recipient you would like the message to go to, just add them to the list as shown here: <strong>user_to = "'batman@world.com', 'robin@world.com'"</strong></li>
                  <li>The way we have used <strong>Net::SMTP.start</strong> it finishes the SMTP session and closes TCP connection.</li>
                  <li>We have also used <strong>begin</strong> and <strong>rescue</strong> for Exception handling.</li>
                </ul>

                <p class="post-note">The above program can be misused and I would strongly advise you not to do the same. Also, I want to make the point that I am writing this article for the purpose of education and awareness. The purpose is NOT to provide someone the information needed to abuse SMTP services.</p>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them. Much of the material on <a href="/">rubylearning.github.io</a> and in the course at <a href="http://rubylearning.org/">rubylearning.org</a> is drawn <strong>primarily</strong> from the <strong>Programming Ruby book</strong>, available from <a href="http://pragprog.com/titles/ruby3/programming-ruby-3">The Pragmatic Bookshelf</a>.</p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">
            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>

</div>

<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
